package org.proteored.miapeapi.xml.msi.adapter;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.proteored.miapeapi.cv.msi.Score;
import org.proteored.miapeapi.cv.msi.ValidationType;
import org.proteored.miapeapi.interfaces.Adapter;
import org.proteored.miapeapi.interfaces.msi.IdentifiedPeptide;
import org.proteored.miapeapi.interfaces.msi.IdentifiedProtein;
import org.proteored.miapeapi.interfaces.msi.ProteinScore;
import org.proteored.miapeapi.xml.msi.autogenerated.MSIIdentifiedProtein;
import org.proteored.miapeapi.xml.msi.autogenerated.ObjectFactory;
import org.proteored.miapeapi.xml.msi.autogenerated.PeptideRefs;
import org.proteored.miapeapi.xml.msi.autogenerated.Ref;
import org.proteored.miapeapi.xml.msi.util.MSIControlVocabularyXmlFactory;
import org.proteored.miapeapi.xml.util.MiapeXmlUtil;

public class IdentifiedProteinAdapter implements Adapter<MSIIdentifiedProtein> {
	private final IdentifiedProtein protein;
	private final ObjectFactory factory;
	private final MSIControlVocabularyXmlFactory cvFactory;

	private static Logger log = Logger.getLogger("log4j.logger.org.proteored");

	public IdentifiedProteinAdapter(IdentifiedProtein identifiedprotein,
			ObjectFactory factory, MSIControlVocabularyXmlFactory cvFactory) {
		this.protein = identifiedprotein;
		this.factory = factory;
		this.cvFactory = cvFactory;
	}

	@Override
	public MSIIdentifiedProtein adapt() {

		MSIIdentifiedProtein proteinXML = factory.createMSIIdentifiedProtein();
		proteinXML.setAC(protein.getAccession());
		proteinXML.setAInformation(protein.getAdditionalInformation());
		proteinXML.setCoverage(protein.getCoverage());
		proteinXML.setDescription(protein.getDescription());
		final Set<ProteinScore> scores = protein.getScores();
		if (scores != null) {
			for (ProteinScore proteinScore : scores) {
				proteinXML.getProteinScore().add(
						cvFactory.createCV(proteinScore.getName(),
								proteinScore.getValue(),
								Score.getInstance(cvFactory.getCvManager())));
			}

		}
		proteinXML.setId(MiapeXmlUtil.IdentifierPrefixes.PROTEIN.getPrefix()
				+ protein.getId());

		// PEPTIDE REFS
		List<IdentifiedPeptide> identifiedPeptides = protein
				.getIdentifiedPeptides();
		if (identifiedPeptides != null && !identifiedPeptides.isEmpty()) {
			PeptideRefs pepRefs = factory.createPeptideRefs();

			for (IdentifiedPeptide peptide : identifiedPeptides) {
				Ref ref = factory.createRef();
				ref.setId(MiapeXmlUtil.IdentifierPrefixes.PEPTIDE.getPrefix()
						+ peptide.getId());
				pepRefs.getRef().add(ref);
			}
			if (!pepRefs.getRef().isEmpty())
				proteinXML.setPeptideRefs(pepRefs);
		} else {
			log.info("Protein without peptide references: "
					+ protein.getAccession());
		}
		proteinXML.setMatched(protein.getPeaksMatchedNumber());
		proteinXML.setPeptideNumber(protein.getPeptideNumber());
		proteinXML.setUnmatched(protein.getUnmatchedSignals());
		proteinXML.setValidationStatus(protein.getValidationStatus());
		proteinXML.setValidationType(cvFactory.createCV(
				protein.getValidationType(), null,
				ValidationType.getInstance(cvFactory.getCvManager())));
		proteinXML.setValidationValue(protein.getValidationValue());

		return proteinXML;
	}

}
